package com.app.motion.parallaximageviewmotion;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    private ListView mListView;
    private int[] mResId = new int[]{R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4,
            R.drawable.pic5, R.drawable.pic6, R.drawable.pic7, R.drawable.pic8, R.drawable.pic9,
            R.drawable.pic10, R.drawable.pic11, R.drawable.pic12, R.drawable.pic13, R.drawable.pic14,
            R.drawable.pic15, R.drawable.pic16, R.drawable.pic17};
    private Bitmap[] mBitmaps = new Bitmap[mResId.length];

    private LayoutInflater mLayoutInflater = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        mLayoutInflater = LayoutInflater.from(this);
        mListView = this.findViewById(R.id.listview);
        mListView.setFastScrollEnabled(false);
        mListView.setVerticalScrollBarEnabled(false);
        mListView.setAdapter(new MyAdapter());
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                Log.e("wanghaha", "first:" + firstVisibleItem + ",visible count:" + visibleItemCount);
                for (int i = 0; i < firstVisibleItem + visibleItemCount; i++) {
                    ViewGroup itemView = (ViewGroup) view.getChildAt(i);
                    if (itemView != null) {
                        OnMyScrollListener listener = itemView.findViewById(R.id.parallax_img);
                        listener.onMyScroll(view, itemView);
                    }
                }
            }
        });
    }


    private void initData() {
        Resources res = this.getResources();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 16;
        for (int i = 0; i < mResId.length; i++) {
            mBitmaps[i] = BitmapFactory.decodeResource(res, mResId[i], options);
        }
    }

    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mResId.length;
        }

        @Override
        public Object getItem(int position) {
            return mResId[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.item, null);
                ViewHolder holder = new ViewHolder();
                holder.imageView = convertView.findViewById(R.id.parallax_img);
                convertView.setTag(holder);
            }
            ViewHolder myHolder = (ViewHolder) convertView.getTag();
            myHolder.imageView.setImageBitmap(mBitmaps[position]);
            return convertView;
        }
    }

    class ViewHolder {

        public ImageView imageView;

    }


    interface OnMyScrollListener {
        void onMyScroll(AbsListView listView, View itemView);
    }


}
